//
//  BaseViewController.swift
//  Browser
//
//  Created by 鹏鹏 on 2021/7/3.
//

import UIKit

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if self.responds(to: #selector(setter: edgesForExtendedLayout)) {
            self.edgesForExtendedLayout = []
        }

        self.view.backgroundColor = KDefine.Color.bgColor

        Logger.printLog(self)
    }

}

extension BaseViewController {

    /// 弹出提示框 - (传入[UIAlertAction])
    /// - Parameters:
    ///   - actions: 传入actions数组
    func showAlert(title: String? = nil, message: String, actions: [UIAlertAction]) {

        if actions.count == 0 { return }

        let alertController = UIAlertController.init(title: title, message: message, preferredStyle: .alert)
        for action in actions {
            alertController.addAction(action)
        }

        self.present(alertController, animated: true, completion: nil)
    }

    /// 弹出提示框 - (传入 取消 / 确认 事件)
    /// - Parameters:
    ///   - cancelAction: 取消
    ///   - confirmAction: 确认
    func showAlert(title: String? = nil, message: String, cancelAction: UIAlertAction? = nil, confirmAction: UIAlertAction? = nil) {

        var actions: [UIAlertAction] = []
        if let cancelAction = cancelAction {
            actions.append(cancelAction)
        }
        if let confirmAction = confirmAction {
            actions.append(confirmAction)
        }

        self.showAlert(title: title, message: message, actions: actions)
    }

    /// 弹出提示框 - (传入 取消 / 确认 标题)
    /// - Parameters:
    ///   - cancelTitle: 取消标题
    ///   - cancelAction: 取消事件
    ///   - confirmTitle: 确认标题
    ///   - confirmAction: 确认事件
    func showAlert(title: String? = nil, message: String, cancelTitle: String, cancelAction: ((UIAlertAction) -> Void)? = nil, confirmTitle: String, confirmAction: ((UIAlertAction) -> Void)? = nil) {

        let cancel = UIAlertAction.init(title: cancelTitle, style: .cancel, handler: cancelAction)
        let confirm = UIAlertAction.init(title: confirmTitle, style: .default, handler: confirmAction)

        self.showAlert(title: title, message: message, cancelAction: cancel, confirmAction: confirm)
    }

    /// 弹出提示框 - (传入 确认 标题)
    /// - Parameters:
    ///   - confirmTitle: 确认标题
    ///   - confirmAction: 确认事件
    func showAlert(title: String? = nil, message: String, confirmTitle: String, confirmAction: ((UIAlertAction) -> Void)? = nil) {

        let confirm = UIAlertAction.init(title: confirmTitle, style: .default, handler: confirmAction)

        self.showAlert(title: title, message: message, confirmAction: confirm)
    }
}
